<template>
    <div class="login-container">
      <div class="login_box">
        <div class="header_img">
          <img src="../assets/logo.png">
        </div>
        <!--登陆区域 -->
        <el-form class='login_form' :model="loginForm" :rules="loginFormRules" ref="loginFormRef">
          <el-form-item prop="username">
            <el-input prefix-icon="iconfont icon-user" v-model="loginForm.phone"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="iconfont icon-3702mima" type="password" v-model="loginForm.password"></el-input>
            <el-button type="primary" @click="sendCode">获取验证码</el-button>
          </el-form-item>
          <el-form-item class="btns">
            <el-button type="primary" @click="checkCode">登陆</el-button>
            <el-button type="info" @click="clearForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        phone: '18324108209',
        password: ''
      },
      loginFormRules: {
        phone: [
          { required: true, message: '请输入手机号码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    clearForm () {
      this.loginForm.phone = ''
      this.loginForm.password = ''
    },
    // 手机登陆
    login () {
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.get('/login/cellphone', this.loginForm)
        console.log(res)
      })
    },
    // 发送验证码
    sendCode () {
      this.$http.get('/captcha/sent?phone=' + this.loginForm.phone)
    },
    // 验证验证码是否真确
    async checkCode () {
      const { data: res } = await this.$http.get(`/captcha/verify?phone=${this.loginForm.phone}&captcha=${this.loginForm.password}`)
      console.log(res)
      if (res.code !== 200) {
        return this.$message.error('验证码错误！')
      }
      this.$message.success('登陆成功')
      this.$router.push('/home')
    }
  }

}
</script>

<style lang = "less" scoped>
.login-container{
  background-color: #2b4b6b;
  height: 100%;
}
  .login_box{
    width: 450px;
    height: 300px;
    border-radius: 3px;
    background-color: #cccccc;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .header_img {
      height: 130px;
      width: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;

      img {
        height: 100%;
        width: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    .login_form{
      /*将文本框放置在下层*/
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 0 20px;
      box-sizing: border-box;  /*规定两个并排的带边框的框： */
    }
    .btns{
      display: flex;
      justify-content: flex-end;
    }
  }
</style>
